package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class BrandCategoryImpl implements IBrandCategoryService {

    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void addNew(BrandCategoryAddNewDTO brandCategoryAddNewDTO){
        log.debug("开始处理添加品牌与类别的关联请求");

        Long brandId = brandCategoryAddNewDTO.getBrandId();
        BrandStandardVO standardById = brandMapper.getStandardById(brandId);
        if (standardById == null ){
            String message = "添加失败,这个品牌不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Long categoryId = brandCategoryAddNewDTO.getCategoryId();
        CategoryStandardVO standardById1 = categoryMapper.getStandardById(categoryId);
        if (standardById1 == null ){
            String message = "添加失败,这个类别不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int rows = brandCategoryMapper.countByBrandAndCategory(brandId,categoryId);
        if (rows==1){
            String message = "添加失败,数据重复";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        LocalDateTime time = LocalDateTime.now();
        BrandCategory brandCategory = new BrandCategory();
        brandCategory.setGmtCreate(time);
        BeanUtils.copyProperties(brandCategoryAddNewDTO,brandCategory);

        rows = brandCategoryMapper.insert(brandCategory);
        if (rows != 1 ){
            String message = "添加失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long brandId, Long categoryId){
        log.debug("处理解除品牌与类别的关联业务");

        int rows = brandCategoryMapper.countByBrandAndCategory(brandId, categoryId);

        if (rows!=1){
            String message = "解除失败，没有这个关联数据";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Long id = brandCategoryMapper.selectById(brandId,categoryId);
        rows = brandCategoryMapper.deleteById(id);
        if (rows != 1 ){
            String message = "解除绑定失败,服务器繁忙";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }
}
